package com.zhao.fishingguideserver.mapper;

import com.zhao.fishingguideserver.POJO.OperationLog;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 操作日志数据访问接口
 */
@Mapper
public interface OperationLogMapper {
    
    /**
     * 插入操作日志
     */
    @Insert("INSERT INTO operation_log(admin_id, admin_name, operation_type, operation_module, " +
            "operation_detail, ip_address, created_at) " +
            "VALUES(#{adminId}, #{adminName}, #{operationType}, #{operationModule}, " +
            "#{operationDetail}, #{ipAddress}, now())")
    @Options(useGeneratedKeys = true, keyProperty = "logId")
    int insert(OperationLog operationLog);
    
    /**
     * 查询操作日志（分页）
     */
    @Select("<script>" +
            "SELECT log_id as logId, admin_id as adminId, admin_name as adminName, " +
            "operation_type as operationType, operation_module as operationModule, " +
            "operation_detail as operationDetail, ip_address as ipAddress, " +
            "created_at as createdAt " +
            "FROM operation_log " +
            "<where>" +
            "<if test='adminName != null and adminName != \"\"'>" +
            "AND admin_name LIKE CONCAT('%', #{adminName}, '%')" +
            "</if>" +
            "<if test='operationType != null and operationType != \"\"'>" +
            "AND operation_type = #{operationType}" +
            "</if>" +
            "<if test='operationModule != null and operationModule != \"\"'>" +
            "AND operation_module = #{operationModule}" +
            "</if>" +
            "</where>" +
            "ORDER BY created_at DESC " +
            "</script>")
    List<OperationLog> selectAll(@Param("adminName") String adminName, 
                                 @Param("operationType") String operationType, 
                                 @Param("operationModule") String operationModule);
    
    /**
     * 根据ID查询操作日志
     */
    @Select("SELECT log_id as logId, admin_id as adminId, admin_name as adminName, " +
            "operation_type as operationType, operation_module as operationModule, " +
            "operation_detail as operationDetail, ip_address as ipAddress, " +
            "created_at as createdAt " +
            "FROM operation_log WHERE log_id = #{logId}")
    OperationLog selectById(Long logId);
}

